﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TotalApplication.Core;

namespace TotalApplication.AngelSix.views
{
    /// <summary>
    /// TreeVIews.xaml 的交互逻辑
    /// </summary>
    public partial class TreeVIews : Window
    {
        public TreeVIews()
        {
            InitializeComponent();

            DataContext = new Class1();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //Directory.GetLogicalDrives().ToList().ForEach(drive =>
            //{
            //    TreeViewItem item = new TreeViewItem();
            //    item.Header = drive;
            //    item.Tag = drive;
            //    item.Items.Add(null);
            //    item.Expanded += Folder_Expanded;
            //    treeView.Items.Add(item);
            //});

            foreach (var drive in Directory.GetLogicalDrives())
            {
                var item = new TreeViewItem()
                {
                    Header = drive,
                    Tag = drive
                };

                item.Items.Add(null); // Placeholder for lazy loading

                item.Expanded += Folder_Expanded; // Event handler for expanding the folder

                tree.Items.Add(item);
            }
        }

        private void Folder_Expanded(object sender, RoutedEventArgs e)
        {
            var item = sender as TreeViewItem;

            // Ensure 'item' is not null before accessing its properties
            if (item == null || item.Items.Count != 1 || item.Items[0] != null)
            {
                return; // Prevent reloading if already populated or 'item' is null
            }

            item.Items.Clear(); // Clear the placeholder item

            var fullPath = item.Tag as string;

            #region Get Directories

            List<string> directories = new List<string>();

            try
            {
                var dirs = Directory.GetDirectories(fullPath ?? "");

                if (dirs.Length > 0)
                {
                    directories.AddRange(dirs);
                }


            }
            catch (Exception )
            {
                //MessageBox.Show($"Error accessing {fullPath}: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return; // Exit if there's an error accessing the directory
            }

            foreach (var dir in directories)
            {
                var subItem = new TreeViewItem()
                {
                    Header = System.IO.Path.GetFileName(dir),
                    Tag = dir
                };
                subItem.Items.Add(null); // Placeholder for lazy loading
                subItem.Expanded += Folder_Expanded; // Event handler for expanding the folder
                item.Items.Add(subItem);
            }

            #endregion

            #region Get Files

            List<string> Files = new List<string>();

            try
            {
                var files = Directory.GetFiles(fullPath ?? "");

                if (files.Length > 0)
                {
                    Files.AddRange(files);
                }


            }
            catch (Exception )
            {
                //MessageBox.Show($"Error accessing {fullPath}: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return; // Exit if there's an error accessing the directory
            }

            Files.ForEach(file =>
            {
                var subItem = new TreeViewItem()
                {
                    Header = System.IO.Path.GetFileName(file),
                    Tag = file
                };

                item.Items.Add(subItem);
            });

            #endregion

        }
    }
}
